import instance from './header.js'

/**
 * post 请求方法
 * @param url
 * @param data
 * @returns {Promise}
 */
function post(url, data) {
    return new Promise((resolve, reject) => {
        instance.post(url, data)
            .then(response => {
                resolve(response)
            })
            .catch(err => {
                reject(err)
            })
    })
}

/**
 * get 请求方法
 * @param url
 * @param data
 * @returns {Promise}
 */
function get(url, data) {
    return new Promise((resolve, reject) => {
        instance.get(url, { params: data })
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
    })
}

/**
 * put 请求方法
 * @param url
 * @param id
 * @param data
 * @returns {Promise}
 */
function put(url, id, data) {
    return new Promise((resolve, reject) => {
        instance.put(url + id, data)
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
    })
}

/**
 * delete 请求方法
 * @param url
 * @param id
 * @returns {Promise}
 */
function _delete(url, id) {
    return new Promise((resolve, reject) => {
        instance.delete(url + id)
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
    })
}
export default { post, get, put, _delete }